package com.linghang.association.controller;
import com.linghang.association.domain.DepartInfo;
import com.linghang.association.domain.Minister;
import com.linghang.association.req.*;
import com.linghang.association.resp.CommonResp;
import com.linghang.association.resp.UserLoginResp;
import com.linghang.association.resp.ViewAdminResp;
import com.linghang.association.service.UserService;
import com.linghang.association.utils.JwtUtil;
import com.linghang.association.websocket.WebSocketServer;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
 * @author LinyaT052
 * @date 2022年07月17日 12:03
 * 用户相关功能接口(包含用户端和管理员端)
 */
/**
 * @RestController注解一般用于返回一个字符串
 * @Controller注解一般用于返回一个页面
 */
@RequestMapping
@RestController
public class UserController {
    /**
     * 共同接口
     */
    @Resource
    private UserService userService;
    @Resource
    private WebSocketServer webSocketServer;
    //用户登录接口
    @PostMapping("/login")
    public CommonResp login(@RequestBody UserLoginReq req){
        CommonResp<Map> resp = new CommonResp<>();
        Map<String,Object> map = new HashMap<>();
        //登陆成功生成token
        UserLoginResp log_rep = userService.login(req);
        Map<String,String> payload = new HashMap<>();
        try {
            //向负载中存放信息生成对应token
            payload.put("id", Integer.toString(log_rep.getId()));
            payload.put("username", log_rep.getUsername());
            payload.put("departmentId",Integer.toString(log_rep.getDepartment()));
            //调用JWT工具类，生成JWT令牌
            String token = JwtUtil.getToken(payload);
            map.put("state", true);
            map.put("username",log_rep.getUsername());
            map.put("departmentId",log_rep.getDepartment());
            map.put("msg", "认证成功");
            map.put("token", token);
        }catch (Exception e){
            map.put("state",false);
            map.put("msg",e.getMessage());
        }
        resp.setContent(map);
        return resp;
    }
    //用户修改密码接口
    @PostMapping("/login/reset-password")
    public CommonResp resetPassword(UserResetPasswordReq req){
        CommonResp resp = new CommonResp();
        userService.resetPassword(req);
        return resp;
    }


    //社团报名接口
    @PostMapping("/login/sign-up")
    public CommonResp sign_up(@RequestBody UserSignUpReq req){
        CommonResp<Map> resp = new CommonResp<>();
        Map<String,Object> map = new HashMap<>();
        userService.sign_in(req);
        map.put("msg","报名成功");
        map.put("state",true);
        resp.setContent(map);
        return  resp;
    }
    /**
     * 管理员接口
     */

    /**
     * 查看所有用户详细信息
     * 权限等级：一级
     * @return
     */
    @GetMapping("/login/admin/view-info-all")
    public CommonResp ViewUserInfo(HttpServletRequest request,PageReq req){
        CommonResp resp = new CommonResp();
        resp.setContent(userService.ViewInfoAll(request,req));
        return resp;
    }
    /**
     * 审核接口
     * 权限等级：二级
     * @return
     */
    @GetMapping("/login/admin/verify")
    public CommonResp VerifySignUp(HttpServletRequest request,Integer status){
        CommonResp resp = new CommonResp();
        Map<String,Object> map = new HashMap<>();
        userService.VerifySignUp(request,status);
        map.put("status","审核成功");
        resp.setContent(map);
        return resp;
    }
    /**
     * 查看自身所在部门信息
     * 权限等级：二级
     * @param req
     * @return
     */
    @GetMapping("/login/admin/view-depart-info")
    public CommonResp ViewDepartInfo(HttpServletRequest request,PageReq req){
        CommonResp resp = new CommonResp();
        resp.setContent(userService.ViewDepartInfo(request,req));
        return resp;
    }
    /**
     * 增删改管理员信息
     * 权限等级：一级
     * 也就是总管理员
     */
    @RequestMapping("/login/admin/edit-admin-info")
    public CommonResp EditAdminInfo(HttpServletRequest request,String username,Integer roleId){
        CommonResp resp = new CommonResp();
        userService.EditAdminInfo(request,username,roleId);
        return resp;
    }
    /**
     * 生成管理员端的账号密码
     */
    @RequestMapping("/login/admin/new-admin")
    public CommonResp newAdminister(HttpServletRequest request, @RequestBody NewAdminReq req){
        CommonResp resp = new CommonResp();
        userService.newAdminister(request,req);
        Map<String,Object>map = new HashMap<>();
        map.put("msg","生成成功！");
        map.put("username",req.getUsername());
        return resp;
    }
    /**
     * 编辑对应社团信息
     */
    @RequestMapping("/login/admin/edit-depart-info")
    public CommonResp EditDepartInfo(HttpServletRequest request,@RequestBody DepartInfo departInfo){
        CommonResp resp = new CommonResp();
        userService.EditDepartInfo(request,departInfo);
        resp.setMessage("部门信息已成功更新");
        return resp;
    }
    /**
     * 编辑部长信息
     */
    @RequestMapping("/login/admin/edit-minister-info")
    public CommonResp EditMinisterInfo(HttpServletRequest request,@RequestBody Minister minister){
        CommonResp resp = new CommonResp();
        userService.EditMinisterInfo(request,minister);
        resp.setMessage("部长信息已成功更新");
        return resp;
    }

    /**
     * 管理员给报名表评分接口
     * @param id
     * @return
     */
    @RequestMapping("/login/admin/give-score")
    public CommonResp giveScore(HttpServletRequest request,@RequestBody Integer id,@RequestBody Integer score){
        CommonResp resp = new CommonResp();
        userService.giveScore(request,id,score);
        resp.setMessage("评分成功");
        return resp;
    }

    /**
     * 查看管理员信息
     * @param request
     * @return
     */
    @RequestMapping("/login/admin/view-admin-info")
    public CommonResp viewAdmin(HttpServletRequest request,PageReq req){
        CommonResp resp = new CommonResp();
        List<ViewAdminResp> admins = userService.viewAdmin(request,req);
        resp.setMessage("管理员信息展示");
        resp.setContent(admins);
        return resp;
    }

    /**
     * 管理员发布公告
     * @param notice
     * @return
     */
    @RequestMapping("/login/admin/release")
    public CommonResp releaseNotice(HttpServletRequest request,String notice){
        CommonResp resp = new CommonResp();
        userService.releaseNotice(request,notice);
        resp.setMessage("公告成功发布");
        return resp;
    }
    /**
     * 发送邮件接口
     * @param qq
     * @return
     */
    @RequestMapping("/login/admin/send-email")
    public CommonResp sendEmail(String qq){
        CommonResp resp = new CommonResp();
        userService.sendEmail(qq+"@qq.com");
        return resp;
    }
}

